﻿using BusinessLogic.BusinessEntity.Entities;
using BusinessLogic.Connectivity.Entities;
using System.Collections.Generic;
using System.ServiceModel;
using BusinessLogic.Connectivity.Interfaces;
using System;
using BusinessLogic;

namespace BankApplication.BusinessLogic
{
    public static class UsersManager
    {

		public static int CreateUser(User user, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.CreateUser(user, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}

			return serverResponse.Payload.ID;
		}


		public static IList<User> GetAllUsers(string sessionTicket)
		{	
			var serverResponse = ServerProxy.Instance.GetAllUsers(sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}

			return serverResponse.Payload;
		}


		public static void SetUserPublicKey(int userID, string publicKey, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.SetUserPublicKey(userID, publicKey, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}


		public static void DeleteUser(int userID, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.DeleteUser(userID, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}

		public static void UpdateUser(User user, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.UpdateUser(user, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}



		public static void AssignRole(int userID, Role role, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.AssignRole(userID, role, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}


		public static void DeleteRole(int userID, Role role, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.DeleteRole(userID, role, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}


		public static IList<Role> GetUserRoles(int userID, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.GetUserRoles(userID, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}

			return serverResponse.Payload;
		}


		public static IDictionary<User, IList<Role>> GetUsersRolesPairs(string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.GetUsersRolePairs(sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}

			return serverResponse.Payload;
		}


		public static IList<Role> GetRoles()
		{
			var serverResponse = ServerProxy.Instance.GetRoles();

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}

			return serverResponse.Payload;
		}

    }
}
